package com.kuang.demo01;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.net.URL;

/**实现图片下载的练习
 * @author FHL
 */
public class TestThread2 extends Thread {
    //保存地址
    private String url;
    //保存名字
    private String name;
    //构造器
    public  TestThread2(String url,String name){
        this.url=url;
        this.name=name;
    }
    /**
     * 重写run（）方法，这是一个线程的执行体
     */
    @Override
    public void run() {
WebDownLoad webDownLoad=new WebDownLoad();
webDownLoad.downLoad(url,name);
        System.out.println("下载"+name+"图片成功！");
    }
}



/**
 * 自定义一个下载器
 */
class WebDownLoad{

    /**
     * 自定义下载方法
     */
    public void downLoad(String url,String name){
        try {
            FileUtils.copyURLToFile(new URL(url),new File(name));
        } catch (IOException e) {
           // e.printStackTrace();
            System.out.println("文件下载异常,io异常，download方法");
        }
    }
    public static void main(String[] args) {
        TestThread2 p1=new TestThread2("https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/4bd5ab12dcff4c8095476bf0b4262f44~tplv-k3u1fbpfcp-zoom-1.image?imageslim","p1");
        TestThread2 p2=new TestThread2("https://gold-cdn.xitu.io/asset/twemoji/2.6.0/svg/1f600.svg","p2");
        TestThread2 p3=new TestThread2("https://sf6-scmcdn2-tos.pstatp.com/xitu_juejin_web/img/weibo.2076a57.svg","p3");
        p1.start();
        p2.start();
        p3.start();
    }
}

